package value

import (
	"context"
	"fmt"
	"tongquetai_server/activity_vk/domain/manage/common/static_data"
)

type TqtTask struct {
	No         int        `json:"no" header:"序号" validate:"gt=0"`
	TypName    string     `json:"typName" header:"任务类型" validate:"required"`
	Typ        int        `json:"typ" skipField:"true" validate:"gt=0"`
	RequireVal int        `json:"requireVal" header:"任务要求"  validate:"gte=0"`
	Award      []TqtAward `json:"award" header:"奖励内容" validate:"gt=0,dive"`
}

func (t *TqtTask) Call(ctx context.Context) (err error) {
	actType := ctx.Value("actType").(int)
	taskMap, err := static_data.GetTaskTypeInfo().GetNameIdMap(ctx, actType)
	if err != nil {
		return
	}
	taskId, ok := taskMap[t.TypName]
	if !ok {
		return fmt.Errorf("任务类型[%s]不存在", t.TypName)
	}
	t.Typ = taskId
	return
}

type SvrTask struct {
	ShowOutsideId string        `json:"show_outside_id"`
	Item          []SvrTaskItem `json:"item"`
}
type SvrTaskItem struct {
	No         int        `json:"no"`
	Typ        int        `json:"typ"`
	RequireVal int        `json:"require_val"`
	Award      []SvrAward `json:"award"`
}

func ToSvrTask(task []TqtTask, ShowOutside TqtItem) SvrTask {
	res := make([]SvrTaskItem, len(task))
	for i, v := range task {
		res[i] = SvrTaskItem{
			No:         v.No,
			Typ:        v.Typ,
			RequireVal: v.RequireVal,
			Award:      TqtToSvrAwards(v.Award),
		}
	}

	return SvrTask{
		ShowOutsideId: TqtToSvrItem(ShowOutside).GameObjectId,
		Item:          res,
	}
}

func ToTqtTask(ctx context.Context, data SvrTask) (task []TqtTask, showOutside TqtItem, err error) {
	actType := ctx.Value("actType").(int)
	taskMap, err := static_data.GetTaskTypeInfo().GetIdNameMap(ctx, actType)
	if err != nil {
		return
	}
	task = make([]TqtTask, len(data.Item))
	for i, v := range data.Item {
		task[i] = TqtTask{
			No:         v.No,
			Typ:        v.Typ,
			TypName:    taskMap[v.Typ],
			RequireVal: v.RequireVal,
			Award:      SvrToTqtAwards(ctx, v.Award),
		}
	}
	showOutside = SvrToTqtItem(ctx, SvrItem{GameObjectId: data.ShowOutsideId})
	return
}
